package com.ojoin.trade.common.utils.excel;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PushbackInputStream;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
import org.apache.poi.hssf.record.CellValueRecordInterface;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ExcelReaderUtil {

	public static final String EMPTY_CELL = "";
	private static final byte[] BIFF8_FILE_HEADER = new byte[] { (byte) 0xD0,
			(byte) 0xCF, 0x11, (byte) 0xE0 };
	public static final byte[] ZIP_FILE_HEADER = new byte[] { 0x50, 0x4b, 0x03,
			0x04 };

	/**
	 * 是否是 excel 2003 的biff8 文件头 eg:
	 * <p/>
	 * PushbackInputStream pis = new PushbackInputStream(new
	 * FileInputStream("jxl.out.xls"), 4); byte[] header = new byte[4];
	 * pis.read(header); pis.unread(header);
	 * 
	 * @param header
	 *            文件的前4个字节
	 * @return
	 */
	public static boolean isBiff8Header(byte[] header) {
		return BIFF8_FILE_HEADER[0] == header[0]
				&& BIFF8_FILE_HEADER[1] == header[1]
				&& BIFF8_FILE_HEADER[2] == header[2]
				&& BIFF8_FILE_HEADER[3] == header[3];
	}

	/**
	 * 是否是 excel 2007 的 Open Xml(zip) 文件头
	 * @param header  文件的前4个字节
	 * @return
	 */
	public static boolean isZipHeader(byte[] header) {
		return ZIP_FILE_HEADER[0] == header[0]
				&& ZIP_FILE_HEADER[1] == header[1]
				&& ZIP_FILE_HEADER[2] == header[2]
				&& ZIP_FILE_HEADER[3] == header[3];
	}

	public static boolean isDateRecord(
			FormatTrackingHSSFListener formatListener,
			CellValueRecordInterface record) {
		int formatIndex = formatListener.getFormatIndex(record);
		String formatString = formatListener.getFormatString(record);
		return HSSFDateUtil.isADateFormat(formatIndex, formatString);
	}

	public static void readCSV(IRowReader reader, InputStream inp)
			throws Exception {
		CSVReader csv = new CSVReader();
		csv.setRowReader(reader);
		csv.process(inp);
	}
	public static void readCSV(IRowReader reader, InputStream inp,String encodeing)
			throws Exception {
		CSVReader csv = new CSVReader();
		csv.setEncodeing(encodeing);
		csv.setRowReader(reader);
		csv.process(inp);
	}

	public static void readExcel(IRowReader reader, InputStream inp)
			throws Exception {
		if (!inp.markSupported()) {
			inp = new PushbackInputStream(inp, 8);
		}

		if (POIFSFileSystem.hasPOIFSHeader(inp)) {
			Excel2003Reader excel03 = new Excel2003Reader();
			excel03.setRowReader(reader);
			excel03.process(inp);
		} else if (POIXMLDocument.hasOOXMLHeader(inp)) {
			Excel2007Readers excel07 = new Excel2007Readers();
			excel07.setRowReader(reader);
			excel07.process(inp);
		} else {
			throw new Exception("文件格式错误，不是标准Excel文件。");
		}

	}

	/**
	 * 读取Excel文件，可能是03也可能是07版本
	 * 
	 * @param excel03
	 * @param excel07
	 * @param fileName
	 * @throws Exception
	 */
	public static void readExcel(IRowReader reader, String fileName)
			throws Exception {
		InputStream in = new FileInputStream(fileName);
		PushbackInputStream pis = new PushbackInputStream(new FileInputStream(
				fileName), 4);
		byte[] header = new byte[4];
		pis.read(header);
		pis.unread(header);
		// 处理excel2003文件
		if (isBiff8Header(header)) {
			Excel2003Reader excel03 = new Excel2003Reader();
			excel03.setRowReader(reader);
			excel03.process(in);
			// 处理excel2007文件
		} else if (isZipHeader(header)) {
			Excel2007Readers excel07 = new Excel2007Readers();
			excel07.setRowReader(reader);
			excel07.process(in);
		} else {
			throw new Exception("文件格式错误，不是标准Excel文件。");
		}
	}
}